package com.example.dellpc.myapplication;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.xlf.nrl.NsRefreshLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.InstrumentedActivity;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;


/**
 * Created by 刘智强 on 2016/8/5.
 * 启动页
 */
public class IndexActivity extends InstrumentedActivity implements NsRefreshLayout.NsRefreshLayoutController, NsRefreshLayout.NsRefreshLayoutListener, SwipeMenuListView.OnMenuItemClickListener,
        SwipeMenuListView.OnSwipeListener, SwipeMenuListView.OnMenuStateChangeListener {

    private String TAG = "IndexActivity";
    //    private NsRefreshLayout nsRefreshLayout;
    private SwipeMenuListView swipeMenuListView;
    private List<Bean> datas;
//    private  MyAdapter adapter;


    private SwipeAdapter swipeAdapter;
    private Long clickTime=0l;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.indexactivity);

        initView();
        JPushInterface.setAlias(this, "13882230271", new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.e("TAG",i+"");
            }
        });
//       adapter =new MyAdapter(this,datas);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public boolean isPullRefreshEnable() {
        return true;
    }

    @Override
    public boolean isPullLoadEnable() {
        return true;
    }

    @Override
    public void onRefresh() {
        Log.d(TAG, "正在刷新...");
//        nsRefreshLayout.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                nsRefreshLayout.finishPullRefresh();
//
//            }
//        },1000);

    }

    @Override
    public void onLoadMore() {
        Log.d(TAG, "加载更多...");
//        nsRefreshLayout.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                datas.add("L");
//                datas.add("M");
//                datas.add("N");
//                datas.add("O");
//                datas.add("P");
//                datas.add("Q");
//               swipeAdapter.notifyDataSetChanged();
//                nsRefreshLayout.finishPullLoad();
//
//            }
//        },1000);

    }

    public void initView() {
        EventBus.getDefault().register(this);//注册eventbus
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem openItem = new SwipeMenuItem(getApplicationContext());
                // 设置菜单的背景
                openItem.setBackground(new ColorDrawable(Color.BLUE));
                // 宽度  菜单的宽度是一定要有的，否则不显示菜单，笔者就吃了这样的亏
                openItem.setWidth(dp2px(90));
                // 菜单标题
                openItem.setTitle("Open");
                // 标题大小
                openItem.setTitleSize(18);
                // 标题的颜色
                openItem.setTitleColor(Color.BLACK);
                // 添加到menu
                menu.addMenuItem(openItem);

                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                // 设置菜单的背景
                deleteItem.setBackground(new ColorDrawable(Color.RED));
                // 宽度  菜单的宽度是一定要有的，否则不显示菜单，笔者就吃了这样的亏
                deleteItem.setWidth(dp2px(90));
                // 菜单标题
                deleteItem.setTitle("Delete");
                // 标题大小
                deleteItem.setTitleSize(18);
                // 标题的颜色
                deleteItem.setTitleColor(Color.BLACK);
                // 添加到menu
                menu.addMenuItem(deleteItem);
            }

        };

        datas = new ArrayList<>();
        datas.add(new Bean(1, "SlidingMenu"));
        datas.add(new Bean(2, "TabLayout"));
        datas.add(new Bean(3, "Flowlayout"));
        datas.add(new Bean(4, "PinnedSectionListView"));
        datas.add(new Bean(5, "ExpandableListView"));
        datas.add(new Bean(6, "ExpandableSelector"));
        datas.add(new Bean(7, "SideQuiclocation"));
        datas.add(new Bean(8, "XUtils"));
        datas.add(new Bean(9, "CameraActivity"));
        datas.add(new Bean(10, "PhotoView"));
        datas.add(new Bean(11, "EventBus"));
        datas.add(new Bean(12, "OkHttp"));
        datas.add(new Bean(13, "GestureLock"));
        datas.add(new Bean(14, "ZXing(二维码)"));
        datas.add(new Bean(15, "Retrofit(网络框架)"));
        datas.add(new Bean(16, "RecyclerView"));
        datas.add(new Bean(17, "CachData"));
        datas.add(new Bean(18, "Permission"));
        datas.add(new Bean(19, "PtrFrameLayout"));
        datas.add(new Bean(20, "AutoViewpager"));
        datas.add(new Bean(21, "WaterWaveView"));
        datas.add(new Bean(22, "Notification"));
//        nsRefreshLayout= (NsRefreshLayout) findViewById(R.id.nsrefreshlayout);
        swipeMenuListView = (SwipeMenuListView) findViewById(R.id.listview);
        swipeMenuListView.setMenuCreator(swipeMenuCreator);
        swipeAdapter = new SwipeAdapter(this);
        swipeMenuListView.setAdapter(swipeAdapter);
        for (Bean bean : datas) {
            swipeAdapter.addNoNotifyUI(bean.getName());
        }
//        nsRefreshLayout.setRefreshLayoutController(this);
//        nsRefreshLayout.setRefreshLayoutListener(this);
        swipeMenuListView.setOnMenuItemClickListener(this);
        swipeMenuListView.setOnSwipeListener(this);

        swipeMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @Override
    public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
        //index的值就是在SwipeMenu依次添加SwipeMenuItem顺序值，类似数组的下标。
        //从0开始，依次是：0、1、2、3...
        switch (index) {
            case 0:
                Toast.makeText(this, "打开", Toast.LENGTH_LONG).show();

                if (datas.get(position).getId() == 1) {
                    Intent intent2 = new Intent(this, SlidingMenuActivity.class);
                    startActivityForResult(intent2, 100);
                } else if (datas.get(position).getId() == 2) {
                    Intent intent1 = new Intent(this, TablLayoutActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name", datas.get(position).getName());
                    intent1.putExtras(bundle);
                    startActivityForResult(intent1, 100);
                } else if (datas.get(position).getId() == 3) {
                    Intent intent = new Intent(this, FlowLayoutActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 4) {
                    Intent intent = new Intent(this, PinnedSectionListViewActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 5) {
                    Intent intent = new Intent(this, MyExpandableListViewActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 6) {
                    Intent intent = new Intent(this, MyExpandableSelector.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 7) {
                    Intent intent = new Intent(this, SideQuiclocationActitivty.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 8) {
                    Intent intent = new Intent(this, XUtilsActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 9) {
                    Intent intent = new Intent(this, CameraActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 10) {
                    Intent intent = new Intent(this, PhotoViewActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 11) {
                    Intent intent = new Intent(this, EventBusActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 12) {
                    Intent intent = new Intent(this, OkHttpActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 13) {
                    Intent intent = new Intent(this, GestureLockActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 14) {
                    Intent intent = new Intent(this, ZXingActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 15) {
                    Intent intent = new Intent(this, RetrofitActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 16) {
                    Intent intent = new Intent(this, RecyleActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 17) {
                    Intent intent = new Intent(this, CachDataActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 18) {
                    Intent intent = new Intent(this, PermissionActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 19) {
                    Intent intent = new Intent(this, PtrFrameLayoutActivity.class);
                    startActivity(intent);
                } else if (datas.get(position).getId() == 20) {
                    Intent intent = new Intent(this, AutoViewpagerActivity.class);
                    startActivity(intent);
                }else if (datas.get(position).getId() == 21) {
                    Intent intent = new Intent(this, WaterWaveViewActivity.class);
                    startActivity(intent);
                }else if (datas.get(position).getId() == 22) {
                    Intent intent = new Intent(this, NotificationActivity.class);
                    startActivity(intent);
                }

                break;
            case 1:
                datas.remove(position);
                swipeAdapter.remove(position);
                swipeMenuListView.setAdapter(swipeAdapter);
//                datas.clear();
//                for (String s:datas
//                     ) {swipeAdapter.addNoNotifyUI(s);
//                }
                break;
            default:
        }
        return false;
    }

    public int dp2px(float dipValue) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @Override
    public void onSwipeStart(int position) {
        Log.d(TAG, "开始滑动" + position);

    }

    @Override
    public void onSwipeEnd(int position) {
        Log.d(TAG, "滑动停止" + position);
    }

    @Override
    public void onMenuOpen(int position) {

    }

    @Override
    public void onMenuClose(int position) {

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, requestCode + "-" + resultCode);
        if (requestCode == 100) {
            int index = swipeMenuListView.getFirstVisiblePosition();
            View v = swipeMenuListView.getChildAt(0);
            int top = (v == null) ? 0 : v.getTop();
            swipeMenuListView.setSelectionFromTop(index, top);
        }
    }

    public class Bean {
        private int id;
        private String name;

        public Bean(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


    /**
     * 接受post过来的广播
     *
     * @param str
     */
    @Subscriber(tag = "tag")
    public void getEventBus(String str) {
//        Log.e("1111", str);
        Utils.toast(getApplicationContext(), str);
    }

    /**
     * activity销毁的时候注销eventbus
     */
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

//    public void registerMessageReceiver() {
//        MyReceiver myReceiver=new MyReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
////        filter.addAction(MESSAGE_RECEIVED_ACTION);
//        registerReceiver(myReceiver, filter);
//    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (System.currentTimeMillis()-clickTime>2000){
            clickTime=System.currentTimeMillis();
            Utils.toast(getApplicationContext(),"再次点击退出!!!");
        }else {
            finish();
        }


    }
}
